package com.exam.service.impl;

import com.baidu.yun.push.AndroidPushMsgToSingleDevice;
import com.exam.common.Constants;
//import com.exam.configuration.WeixinClent;
import com.exam.dao.OrderDao;
import com.exam.service.OrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;

//    @Autowired
//    private WeixinClent weixinClent;

    @Override
    public List<Map<String, Object>> getOrderList(HttpServletRequest request) {
        String openId = request.getParameter("openId");
        String currentPage = request.getParameter("currentPage");
        Integer current_page = 1;
        Integer page_Size = 10;
        String pageSize = request.getParameter("pageSize");
        if(currentPage != null || !"".equals(currentPage)) {
            try {
                current_page = Integer.parseInt(currentPage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(pageSize != null || !"".equals(pageSize)) {
            try {
                page_Size = Integer.parseInt(pageSize);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PageHelper.startPage(current_page, page_Size);
        Page<Map<String, Object>> orderList = orderDao.getOrderListByOpenId(openId);
        return orderList;
    }

    @Override
    public Map<String, String> saveOrderInfo(HttpServletRequest request) {
        Map<String, String> resultMap = new HashMap<>();
        String wxCode = request.getParameter("wxCode");
//        String resultS = weixinClent.code2Session(Constants.APPID, Constants.APP_SECRET, wxCode,"authorization_code");
        String resultS = "";
        System.out.println("获取微信小程序信息："+resultS);
        if(Constants.isTest&&"023h6zRk27piVD0tvATk2TsORk2h6zRO".equals(wxCode)){
            resultS = "{\"session_key\":\"DPE7kVAONJe8Tv9T8Pch4w==\",\"openid\":\"o5DIs5KH6TaqTMfKBXL-spcru0jw\"}";
        }
        JSONObject json = JSONObject.fromObject(resultS);

        if(json != null && json.containsKey("openid")){

        }



        // 推送消息
        AndroidPushMsgToSingleDevice.sendOrderMsg("aaaa");

        return null;
    }
}
